/*
 * 版权所有 2009-2022山东新北洋信息技术股份有限公司 保留所有权利。
 */
package online.xuping.thousandsOfWorlds.config;

import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置文件加密配置类
 *
 * @author liuzicheng
 * @date 2022/8/27 10:39
 * @copyright 2022 山东新北洋信息技术股份有限公司. All rights reserved
 */
@Slf4j
@Configuration
public class EncryptorConfig {

    //系统加密密钥
    private static final String DEFAULT_SECRET = "{JV-{q2lO]r_^T*fM2&sjk~|dn~0-a01";
    //系统服务名
    @Value("${spring.application.name}")
    private String serviceName;

    @Bean("encryptorBean")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        //两者组合成秘钥
        config.setPassword(serviceName + DEFAULT_SECRET);
        //默认初始化大小 意义不明
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }
}
